﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA
{
    public partial class FrmReportTaskQuantities : BaseForm
    {
        #region Constants
        private const String REPORT_NAME_TASKS_QUANTITY_PROJECTS = "Relatório - Quantidade de Tarefas por Projeto";
        private const String REPORT_NAME_TASKS_QUANTITY_ARCHITECTS = "Relatório - Quantidade de Tarefas por Arquiteto";
        private const String REPORT_NAME_TASKS_QUANTITY_STATES = "Relatório - Quantidade de Tarefas por Status";
        private const String REPORT_NAME_TASKS_QUANTITY_CATEGORIES = "Relatório - Quantidade de Tarefas por Categoria";
        #endregion

        #region Starting Form
        public FrmReportTaskQuantities()
        {
            InitializeComponent();
        }
        private void FrmReportTaskQuantities_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            try
            {
                LoadStates();
            }
            catch (Exception ex)
            {
                HandleException("Iniciar a Tela.", ex);
            }
        }
        #endregion

        #region Events
        private void cmdGenerateReport_Click(object sender, EventArgs e)
        {
            GenerateReport();
        }
        #endregion

        #region Methods
        private void LoadStates()
        {
            try
            {
                BusinessLogic.GTA bl = new BusinessLogic.GTA();
                List<BusinessEntity.TaskDomain> states = bl.ListStates();
                if (states != null)
                    states.ForEach(item => lstStates.Items.Add(item.Description));

            }
            catch (Exception ex)
            {
                HandleException("Carregar a lista de status", ex);
            }
        }
        private void GenerateReport()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                FrmReportViewer rptViewer = null;
                BusinessEntity.TaskFilter filter = GetReportFilter();
                String filterDescription = GetFilterDescription();
                BusinessLogic.GTA bl = new BusinessLogic.GTA();
                if (opTasksForProject.Checked)
                {
                    List<BusinessEntity.InformativeSummaryProjects> summaryProjects = bl.ListInformativeSummaryProjects(Program.LoggedUser, filter);
                    List<ReportEntity.TaskQuantityProjects> list = Translators.BusinessEntityToReportEntity.TaskQuantityTranslator.ToTaskQuantityProjects(summaryProjects);
                    Files.Reports.ReportTaskQuantityForProjects report = new GTA.Files.Reports.ReportTaskQuantityForProjects();
                    report.SetDataSource(list);
                    rptViewer = new FrmReportViewer(report);
                    rptViewer.Text = REPORT_NAME_TASKS_QUANTITY_PROJECTS;
                    report.SetParameterValue("param_filter", filterDescription);
                }
                else if (opTasksForArchitect.Checked)
                {
                    List<BusinessEntity.InformativeSummaryArchitects> summaryArchitects = bl.ListInformativeSummaryArchitects(Program.LoggedUser, filter);
                    List<ReportEntity.TaskQuantityArchitects> list = Translators.BusinessEntityToReportEntity.TaskQuantityTranslator.ToTaskQuantityArchitects(summaryArchitects);
                    Files.Reports.ReportTaskQuantityForArchitects report = new GTA.Files.Reports.ReportTaskQuantityForArchitects();
                    report.SetDataSource(list);
                    rptViewer = new FrmReportViewer(report);
                    rptViewer.Text = REPORT_NAME_TASKS_QUANTITY_ARCHITECTS;
                    report.SetParameterValue("param_filter", filterDescription);
                }
                else if (opTasksForState.Checked)
                {
                    List<BusinessEntity.InformativeSummaryStates> summaryStates = bl.ListInformativeSummaryStates(Program.LoggedUser, filter);
                    List<ReportEntity.TaskQuantityStates> list = Translators.BusinessEntityToReportEntity.TaskQuantityTranslator.ToTaskQuantityStates(summaryStates);
                    Files.Reports.ReportTaskQuantityForStates report = new GTA.Files.Reports.ReportTaskQuantityForStates();
                    report.SetDataSource(list);
                    rptViewer = new FrmReportViewer(report);
                    rptViewer.Text = REPORT_NAME_TASKS_QUANTITY_STATES;
                    report.SetParameterValue("param_filter", filterDescription);
                }
                else if (opTasksForCategory.Checked)
                {
                    List<BusinessEntity.InformativeSummaryCategories> summaryCategories = bl.ListInformativeSummaryCategories(Program.LoggedUser, filter);
                    List<ReportEntity.TaskQuantityCategories> list = Translators.BusinessEntityToReportEntity.TaskQuantityTranslator.ToTaskQuantityCategories(summaryCategories);
                    Files.Reports.ReportTaskQuantityForCategories report = new GTA.Files.Reports.ReportTaskQuantityForCategories();
                    report.SetDataSource(list);
                    rptViewer = new FrmReportViewer(report);
                    rptViewer.Text = REPORT_NAME_TASKS_QUANTITY_CATEGORIES;
                    report.SetParameterValue("param_filter", filterDescription);
                }
                rptViewer.MdiParent = this.MdiParent;
                rptViewer.Show();
            }
            catch (Exception ex)
            {
                HandleException("Gerar relatório de quantidade de tarefas.", ex);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
        private String GetFilterDescription()
        {
            String filterDescription = String.Empty;

            if (txtDateScheduleStartFrom.Checked)
                filterDescription += "Início Previsto de: " + txtDateScheduleStartFrom.Value.ToString(DATE_FORMAT) + "\r\n";
            if (txtDateScheduleStartTo.Checked)
                filterDescription += "Início Previsto até: " + txtDateScheduleStartTo.Value.ToString(DATE_FORMAT) + "\r\n";
            if (txtDateScheduleEndFrom.Checked)
                filterDescription += "Término Previsto de: " + txtDateScheduleEndFrom.Value.ToString(DATE_FORMAT) + "\r\n";
            if (txtDateScheduleEndTo.Checked)
                filterDescription += "Término Previsto até: " + txtDateScheduleEndTo.Value.ToString(DATE_FORMAT) + "\r\n";

            if (txtDateRealizedStartFrom.Checked)
                filterDescription += "Início Realizado de: " + txtDateRealizedStartFrom.Value.ToString(DATE_FORMAT) + "\r\n";
            if (txtDateRealizedStartTo.Checked)
                filterDescription += "Início Realizado até: " + txtDateRealizedStartTo.Value.ToString(DATE_FORMAT) + "\r\n";
            if (txtDateRealizedEndFrom.Checked)
                filterDescription += "Término Realizado de: " + txtDateRealizedEndFrom.Value.ToString(DATE_FORMAT) + "\r\n";
            if (txtDateRealizedEndTo.Checked)
                filterDescription += "Término Realizado até: " + txtDateRealizedEndTo.Value.ToString(DATE_FORMAT) + "\r\n";

            if (lstStates.CheckedItems.Count > 0)
            {
                filterDescription += "Status: ";
                foreach (ListViewItem item in lstStates.CheckedItems)
                    filterDescription += " " + item.Text + ",";
                filterDescription = filterDescription.Remove(filterDescription.Length - 1, 1);
                filterDescription += "\r\n";
            }

            if (String.IsNullOrEmpty(filterDescription))
                filterDescription = "Nenhum";

            return filterDescription;
        }
        private BusinessEntity.TaskFilter GetReportFilter()
        {
            BusinessEntity.TaskFilter filter = new GTA.BusinessEntity.TaskFilter();
            filter.ScheduleEstimatedEndFrom = txtDateScheduleEndFrom.Checked ? txtDateScheduleEndFrom.Value : DateTime.MinValue;
            filter.ScheduleEstimatedEndTo = txtDateScheduleEndTo.Checked ? txtDateScheduleEndTo.Value : DateTime.MinValue;
            filter.ScheduleEstimatedStartFrom = txtDateScheduleStartFrom.Checked ? txtDateScheduleStartFrom.Value : DateTime.MinValue;
            filter.ScheduleEstimatedStartTo = txtDateScheduleStartTo.Checked ? txtDateScheduleStartTo.Value : DateTime.MinValue;
            filter.ScheduleRealizedEndFrom = txtDateRealizedEndFrom.Checked ? txtDateRealizedEndFrom.Value : DateTime.MinValue;
            filter.ScheduleRealizedEndTo = txtDateRealizedEndTo.Checked ? txtDateRealizedEndTo.Value : DateTime.MinValue;
            filter.ScheduleRealizedStartFrom = txtDateRealizedStartFrom.Checked ? txtDateRealizedStartFrom.Value : DateTime.MinValue;
            filter.ScheduleRealizedStartTo = txtDateRealizedStartTo.Checked ? txtDateRealizedStartTo.Value : DateTime.MinValue;
            filter.Project = Program.LoggedUser.UserProfile == BusinessEntity.UserManagement.Profiler.Master ? null : Program.LoggedUser.UserProjects;
            filter.State = GetSelectedStates();
            if (Program.LoggedUser.UserProfile == BusinessEntity.UserManagement.Profiler.User)
            {
                if (filter.DesignedTo == null)
                    filter.DesignedTo = new List<GTA.BusinessEntity.UserManagement.User>();
                filter.DesignedTo.Add(new BusinessEntity.UserManagement.User(Program.LoggedUser.Name));
            }
            return filter;
        }
        private List<BusinessEntity.TaskDomain> GetSelectedStates()
        {
            List<BusinessEntity.TaskDomain> states = null;
            if (lstStates.CheckedItems.Count > 0)
            {
                states = new List<GTA.BusinessEntity.TaskDomain>();
                foreach (ListViewItem item in lstStates.CheckedItems)
                    states.Add(new GTA.BusinessEntity.TaskDomain(0, item.Text));
            }
            return states;
        }
        #endregion
    }
}
